package cn.cdeden.knowledge.controller.recycle.factory;

import cn.cdeden.common.core.domain.R;
import cn.cdeden.common.core.utils.MapstructUtils;
import cn.cdeden.common.core.utils.page.TableDataInfo;
import cn.cdeden.common.mybatis.validator.QueryHelpMybatisPlus;
import cn.cdeden.common.oss.enumd.OssModuleType;
import cn.cdeden.common.satoken.utils.LoginHelper;
import cn.cdeden.knowledge.domain.FilesTreeRecycle;
import cn.cdeden.knowledge.domain.PersonalFilesTree;
import cn.cdeden.knowledge.domain.dto.FilesTreeRecycleParam;
import cn.cdeden.knowledge.domain.vo.FilesSizeType;
import cn.cdeden.knowledge.domain.vo.FilesTreeRecycleVo;
import cn.cdeden.knowledge.enums.DeleteTypeEnums;
import cn.cdeden.knowledge.enums.TreeEnum;
import cn.cdeden.knowledge.enums.TreeTypeEnum;
import cn.cdeden.knowledge.mapper.PersonalFilesTreeMapper;
import cn.cdeden.knowledge.service.*;
import cn.cdeden.knowledge.utils.PageUtil;
import cn.cdeden.resource.api.RemoteFileService;
import cn.cdeden.resource.api.RemoteOssEsService;
import cn.cdeden.resource.api.domain.RemoteEsFilesTree;
import cn.cdeden.system.api.model.LoginUser;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.undertow.util.BadRequestException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@RequiredArgsConstructor
@Component("personalRecycle")
public class RecyclePersonal implements RecycleInterface{

    private final PersonalFilesTreeService filesTreeService;

    private final FilesCollectionService filesCollectionService;

    private final FilesLabelRecordService filesLabelRecordService;

    private final FilesRecentlyViewedService viewedService;

    private final PersonalFilesTreeMapper treeMapper;


    private final FileService fileService;

    private final SysFormDocService sysFormDocService;

    @DubboReference(check = false)
    private final RemoteOssEsService remoteOssEsService;


    @Override
    public boolean deleteBatch(List<String> ids, String info) throws BadRequestException {

        LoginUser user = LoginHelper.getLoginUser();
//        FileUserDto user = FileUserUtils.getUser(); //用户信息
        String username = user.getUsername();
        String userId = Long.toString(user.getUserId());
        List<PersonalFilesTree> treeList = filesTreeService.listByIds(ids);

        if (CollectionUtils.isEmpty(treeList)) {
            throw new BadRequestException("错误的信息删除");
        } else {
            treeList.forEach(filesTree -> {
                if (!StringUtils.equalsIgnoreCase(filesTree.getCreateId(), userId)) {
                    try {
                        throw new BadRequestException("错误的拥有者");
                    } catch (BadRequestException e) {
                        e.printStackTrace();
                    }
                }
            });
            Set<String> setIds = new HashSet<>();
            Set<String> set = deleteBatch(ids, userId, username);
            if (CollectionUtils.isNotEmpty(set)){
                setIds.addAll(set);
            }
            setIds.addAll(ids);
            List<String> list = new ArrayList<>(setIds);
            try {
                filesCollectionService.deleteLibTaggedData("0",list); //删除 收藏
                //删除关注
                //删除标签
                filesLabelRecordService.deleteByFileIds(setIds, TreeEnum.PERSONAL.getType());

                viewedService.deleteByIds(new HashSet<>(list),TreeEnum.PERSONAL.getType());
            }catch (Exception e){
                System.err.println("============================================================");
                e.printStackTrace();
            }

            //假删
            UpdateWrapper<PersonalFilesTree> updateWrapper = new UpdateWrapper<PersonalFilesTree>()
                .set("delete_id", userId).set("delete_by", username)
                .set("delete_type",DeleteTypeEnums.DISPLAY.getType())
                .set("delete_time", new Date())
                .in("id", ids);
            filesTreeService.update(updateWrapper);


            //获取所有数据的esId 删除es中的数据
            List<PersonalFilesTree> list1 = filesTreeService.list(new LambdaQueryWrapper<PersonalFilesTree>()
                .in(PersonalFilesTree::getId, ids.stream().map(Long::parseLong)
                    .collect(Collectors.toList())));
            List<String> esIds = list1.stream().map(PersonalFilesTree::getEsId).collect(Collectors.toList());
            esIds.forEach(v -> {
                try {
                    remoteOssEsService.deleteById(v);
                } catch (Exception e) {

                }

            });


            //Es同步假删
//            EsPersonalFilesTree esPersonalFilesTree = new EsPersonalFilesTree();
//            esPersonalFilesTree
//                    .setDeleteId(userId)
//                    .setDeleteBy(username)
//                    .setDeleteType(DeleteTypeEnums.DISPLAY.getType())
//                    .setDeleteTime(new Date());
//            LambdaEsUpdateWrapper<EsPersonalFilesTree> esUpdateWrapper = new LambdaEsUpdateWrapper<>();
//            esUpdateWrapper.in("id", ids);
//
//            esPersonalFilesTreeMapper.update(esPersonalFilesTree,esUpdateWrapper);

            return true;
        }
    }

    private Set<String> deleteBatch(List<String> ids,String userId,String username) {
        QueryWrapper<PersonalFilesTree> queryWrapper = new QueryWrapper<PersonalFilesTree>().in("parent_id", ids).isNull("delete_id");
        List<PersonalFilesTree> list = filesTreeService.list(queryWrapper);
        if (CollectionUtils.isNotEmpty(list)) {
            Set<String> setId = new HashSet<>();
            List<String> list1 = new ArrayList<>();
            list.forEach(tree -> {
                String of = String.valueOf(tree.getId());
                setId.add(of);
                if (StringUtils.equalsIgnoreCase(TreeTypeEnum.FOLDER.getType(),tree.getType())){
                    list1.add(of);
                }
            });
            filesTreeService.folderCheck(list1,setId,true);
//            throw new BadRequestException("选中的文件夹中有子级，无法删除");
//            setId.forEach(System.out::println);

            UpdateWrapper<PersonalFilesTree> updateWrapper = new UpdateWrapper<PersonalFilesTree>()
                .set("delete_id", userId).set("delete_by", username)
                .set("delete_type", DeleteTypeEnums.NO_DISPLAY.getType())
                .set("delete_time", new Date())
                .in("id", setId);
            filesTreeService.update(updateWrapper);
            return setId;
        }
        return null;
    }

    @Override
    public TableDataInfo<FilesTreeRecycleVo> selectRecycleAll(FilesTreeRecycleParam query, Pageable pageable) {
        IPage<PersonalFilesTree> page = PageUtil.toMybatisPage(pageable);
        List<String> sortDesc = query.getSortDesc();
        List<String> sortAsc = query.getSortAsc();
        if (CollectionUtils.isEmpty(sortDesc) && CollectionUtils.isEmpty(sortAsc)){
            sortDesc = new ArrayList<>();
            sortDesc.add("delete_time");
        }

        QueryWrapper<PersonalFilesTree> predicate = QueryHelpMybatisPlus.getPredicate(query);

        LoginUser user = LoginHelper.getLoginUser();
//        FileUserDto user = FileUserUtils.getUser(); //用户信息
        String username = user.getUsername();
        String userId = Long.toString(user.getUserId());
        predicate.eq("create_id", userId).eq("delete_id", userId)
            .eq("delete_type", DeleteTypeEnums.DISPLAY.getType())
            .orderByDesc("delete_time")
        ;

        IPage<PersonalFilesTree> pageList = treeMapper.selectPage(page, predicate);

        List<PersonalFilesTree> records = pageList.getRecords();
//        List<FilesTreeRecycleVo> treeVoList = getList(records);
        List<FilesTreeRecycleVo> treeVoList = MapstructUtils.convert(records, FilesTreeRecycleVo.class);
//        List<FilesTreeRecycleVo> treeVoList = ConvertUtil.convertList(records, FilesTreeRecycleVo.class);
//        long total = pageList.getTotal();
//        return new PageInfo<>(pageList.getTotal(), treeVoList, pageable.getPageNumber(), pageable.getPageSize());
        return new TableDataInfo<>(treeVoList, pageList.getTotal(), pageable.getPageNumber(), pageable.getPageSize());
    }

    @Override
    public List<FilesTreeRecycle> deleteRecycle(List<String> ids, String type, String teamId) throws BadRequestException {
        List<FilesTreeRecycle> recycleList = null ;
        List<String> list = new ArrayList<>();
        if (StringUtils.equalsIgnoreCase("0",type)){
            if (CollectionUtils.isEmpty(ids)) {
                throw new BadRequestException("删除数据为空");
            }
            recycleList = deleteRecycle(ids);
        }else  if (StringUtils.equalsIgnoreCase("1",type)){
            recycleList = deleteRecycleAll();
        }
//        recycleList.forEach(FilesTreeRecycle::getFileTreeId);
        //删除数据
        List<String> collect = null;
        if ( CollectionUtils.isEmpty(recycleList)) {
            throw new BadRequestException("删除数据为空");
        }

        deleteData(recycleList);
        return recycleList;
    }

    @Override
    public R recoverFileList(List<String> ids, String teamId) throws BadRequestException {
        if (ids.size() > 1){
            return recoverFile(ids);
        }else {
            return recoverFile(ids.get(0));
        }
    }



    public R recoverFile(String id) {
        LoginUser user = LoginHelper.getLoginUser();
//        FileUserDto user = FileUserUtils.getUser(); //用户信息
        String username = user.getUsername();
        String userId = Long.toString(user.getUserId());
        QueryWrapper<PersonalFilesTree> queryWrapper = new QueryWrapper<PersonalFilesTree>();
        queryWrapper.eq("id", id).eq("create_id", userId).isNotNull("delete_id");
        PersonalFilesTree filesTree = filesTreeService.getOne(queryWrapper);

        QueryWrapper<PersonalFilesTree> wrapper = new QueryWrapper<PersonalFilesTree>()
            .eq("id", filesTree.getParentId()).isNull("delete_id");

        PersonalFilesTree personalFile = filesTreeService.getOne(wrapper);
        if (ObjectUtils.isEmpty(personalFile)) {
//            return R.ok("请选择还有位置");
            return R.fail("请选择还有位置");
//            return BaseRespResult.successResult(null, RespResultEnums.HINT_OPERATION,"请选择还有位置");
        }
        boolean b = recoverFile(filesTree, userId, username, String.valueOf(1));
        return R.ok("还原成功");
//        return BaseRespResult.successResult(null, "还原成功");
    }


    public R recoverFile(List<String> ids) throws BadRequestException {
        LoginUser user = LoginHelper.getLoginUser();
//        FileUserDto user = FileUserUtils.getUser(); //用户信息
        String username = user.getUsername();
        String userId = Long.toString(user.getUserId());
        QueryWrapper<PersonalFilesTree> queryWrapper = new QueryWrapper<PersonalFilesTree>();
        queryWrapper.in("id", ids).eq("create_id", userId).isNotNull("delete_id");
        List<PersonalFilesTree> filesTrees = filesTreeService.list(queryWrapper);
        if (CollectionUtils.isEmpty(filesTrees) && filesTrees.size() != ids.size() ) {
            throw new BadRequestException("错误的 有错误的 文件序号");
        }
        List<PersonalFilesTree> success = new ArrayList<>();
        List<PersonalFilesTree> exception = new ArrayList<>();
        // 循环恢复数据
        for (int i = 0; i < filesTrees.size(); i++) {
            PersonalFilesTree filesTree = filesTrees.get(i);
            boolean b = recoverFile(filesTree, userId, username, String.valueOf((i+1)));
            if (b) {
                success.add(filesTree);
            } else {
                exception.add(filesTree);
            }
        };

        if (CollectionUtils.isNotEmpty(exception) && exception.size() > 0) {
            StringBuilder message = new StringBuilder(success.size() + "个对象还原成功,");
            StringJoiner joiner = new StringJoiner(",");
            exception.forEach(tree -> {
                joiner.add(tree.getRealName());
            });
            message.append(joiner);
            message.append("等");
            message.append(exception.size());
            message.append("个对象的原目录文件夹已不存在，请单独进行还原");
//            return R.ok(message.toString());
            return R.fail(message.toString());
//            return BaseRespResult.successResult(null, RespResultEnums.HINT, message.toString());
        } else {
            return R.ok("还原成功");
//            return BaseRespResult.successResult(null, "还原成功");
        }
    }






    public boolean recoverFile(PersonalFilesTree filesTree, String userId, String username ,String num) {
        QueryWrapper<PersonalFilesTree> queryWrapper = new QueryWrapper<PersonalFilesTree>()
            .eq("id", filesTree.getParentId()).isNull("delete_id");

        PersonalFilesTree personalFile = filesTreeService.getOne(queryWrapper);
        if (ObjectUtils.isEmpty(personalFile)) {
            return false;
        }
        String realName = filesTree.getRealName();
        String id = Long.toString(filesTree.getId());


        if (StringUtils.equalsIgnoreCase(TreeTypeEnum.FOLDER.getType(),filesTree.getType())){
            realName += "_恢复文件夹";
        }else if (StringUtils.equalsIgnoreCase(TreeTypeEnum.FILE.getType(),filesTree.getType())){
            realName += "_恢复文件";
        }else {
            realName += "_恢复";
        }

//        DateUtil.getTime(DateTimeFormatter.ofPattern("yyMMddHH:mm"));





//
//        if (!filesTreeService.judgeIsEmpty(id, username, realName, filesTree.getParentId(), filesTree.getType(),filesTree.getStorageId())) {
//            realName += "_" + System.currentTimeMillis();
//        }
        UpdateWrapper<PersonalFilesTree> updateWrapper = new UpdateWrapper<PersonalFilesTree>()
            .set("delete_id", null).set("delete_by", null)
            .set("delete_time", null)
            .set("delete_type", null)
            .set("real_name", realName)
            .set("update_id", userId).set("update_by", username).set("update_time", new Date())
            .eq("id", filesTree.getId());
        boolean update = filesTreeService.update(updateWrapper);


        //还原Es中的数据
        RemoteEsFilesTree remoteEsFilesTree = new RemoteEsFilesTree();
        remoteEsFilesTree.setId(personalFile.getId());
        remoteEsFilesTree.setLibraryType(OssModuleType.Personal.getCode());
        remoteEsFilesTree.setVersionId(personalFile.getEdition());
        remoteEsFilesTree.setVersionName(personalFile.getEditionName());
        remoteEsFilesTree.setSuffix(personalFile.getSuffix());
        remoteEsFilesTree.setType(personalFile.getType());
        remoteEsFilesTree.setStorageId(personalFile.getStorageId());
        remoteEsFilesTree.setSize(personalFile.getSize());
        remoteEsFilesTree.setCreateBy(Long.valueOf(userId));
        remoteEsFilesTree.setCreateTime(new Date());
        remoteEsFilesTree.setUpdateBy(Long.valueOf(userId));
        remoteEsFilesTree.setUpdateTime(new Date());
        remoteEsFilesTree.setRealName(realName);
        remoteEsFilesTree.setParentId(personalFile.getParentId());
        remoteEsFilesTree.setUserIdList(Arrays.asList(userId.toString()));
        try {
            String insert = remoteOssEsService.insert(remoteEsFilesTree);
            //修改个人文件中的esId
            PersonalFilesTree personalFilesTreeUpdate = new PersonalFilesTree();
            personalFilesTreeUpdate.setId(personalFile.getId());
            personalFilesTreeUpdate.setEsId(insert);
            filesTreeService.updateById(personalFilesTreeUpdate);
        } catch (Exception e) {

        }

        //看是否是文件夹 下是否要恢复文件
        if (StringUtils.equalsIgnoreCase(TreeTypeEnum.FOLDER.getType(),filesTree.getType())){
            recoverFile(Long.toString(filesTree.getId()),userId,username);
        }

        return update;
    }



    public boolean recoverFile(String id, String userId, String username) {
        QueryWrapper<PersonalFilesTree> queryWrapper = new QueryWrapper<PersonalFilesTree>()
            .eq("parent_id", id ).isNotNull("delete_id").eq("delete_type",DeleteTypeEnums.NO_DISPLAY.getType());
        List<PersonalFilesTree> treeList = filesTreeService.list(queryWrapper);
        if (CollectionUtils.isEmpty(treeList)){
            return true;
        }

        Set<String> ids = new HashSet<>();
        List<String> idList = new ArrayList<>();
        treeList.forEach(tree -> {
            String of = String.valueOf(tree.getId());
            if (StringUtils.equalsIgnoreCase(TreeTypeEnum.FOLDER.getType(),tree.getType())){
                idList.add(of);
            }
            ids.add(of);
        });

        //查找需要修改文件的
        filesTreeService.folderCheck(idList,  ids,false);

        UpdateWrapper<PersonalFilesTree> updateWrapper = new UpdateWrapper<PersonalFilesTree>()
            .set("delete_id", null).set("delete_by", null)
            .set("delete_time", null)
            .set("delete_type", null)
            .set("update_id", userId).set("update_by", username).set("update_time", new Date())
            .in("id", ids);

        //还原es中的数据
        List<PersonalFilesTree> personalFilesTrees = filesTreeService.list(new LambdaQueryWrapper<PersonalFilesTree>().in(PersonalFilesTree::getId, ids));
        personalFilesTrees.forEach(v -> {
            RemoteEsFilesTree remoteEsFilesTree = new RemoteEsFilesTree();
            remoteEsFilesTree.setId(v.getId());
            remoteEsFilesTree.setLibraryType(OssModuleType.Personal.getCode());
            remoteEsFilesTree.setVersionId(v.getEdition());
            remoteEsFilesTree.setVersionName(v.getEditionName());
            remoteEsFilesTree.setSuffix(v.getSuffix());
            remoteEsFilesTree.setType(v.getType());
            remoteEsFilesTree.setStorageId(v.getStorageId());
            remoteEsFilesTree.setSize(v.getSize());
            remoteEsFilesTree.setCreateBy(Long.valueOf(userId));
            remoteEsFilesTree.setCreateTime(v.getCreateTime());
            remoteEsFilesTree.setUpdateBy(Long.valueOf(userId));
            remoteEsFilesTree.setUpdateTime(v.getUpdateTime());
            remoteEsFilesTree.setRealName(v.getRealName());
            remoteEsFilesTree.setParentId(v.getParentId());
            remoteEsFilesTree.setUserIdList(Arrays.asList(userId.toString()));
            try {
                String insert = remoteOssEsService.insert(remoteEsFilesTree);
                //修改个人文件中的esId
                PersonalFilesTree personalFilesTreeUpdate = new PersonalFilesTree();
                personalFilesTreeUpdate.setId(v.getId());
                personalFilesTreeUpdate.setEsId(insert);
                filesTreeService.updateById(personalFilesTreeUpdate);
            } catch (Exception e) {

            }
        });

        return filesTreeService.update(updateWrapper);
    }





    private List<FilesTreeRecycle> deleteRecycleAll() throws BadRequestException {
        LoginUser user = LoginHelper.getLoginUser();
//        FileUserDto user = FileUserUtils.getUser(); //用户信息
        String username = user.getUsername();
        String userId = Long.toString(user.getUserId());
        QueryWrapper<PersonalFilesTree> wrapper = new QueryWrapper<PersonalFilesTree>()
            .eq("delete_id", userId)
//                .eq("delete_type",DeleteTypeEnums.DISPLAY.getType())
            ;
        List<PersonalFilesTree> treeList = filesTreeService.list(wrapper);
        if (CollectionUtils.isEmpty(treeList)) {
            throw new BadRequestException("没有要删除的对象");
        }
        return deleteRecycleActual(userId,treeList,false);
    }



    private void  deleteData( List<FilesTreeRecycle> recycleList) throws BadRequestException {
        List<Long> ids = new ArrayList<>();
        for (FilesTreeRecycle tree : recycleList) {
            if (StringUtils.equalsIgnoreCase(TreeTypeEnum.FILE.getType(),tree.getType())){
                PersonalFilesTree recycle = MapstructUtils.convert(tree, PersonalFilesTree.class);
//                PersonalFilesTree recycle = ConvertUtil.convert(tree, PersonalFilesTree.class);
                recycle.setId(tree.getFileTreeId());
//                System.out.println("----");
                // 删除已用空间
                fileService.upDateFolderSizeUpDate(String.valueOf(LoginHelper.getUserId()),recycle,null,tree.getSize(), FilesSizeType.DeleteSize);
//                fileService.upDateFolderSizeUpDate(String.valueOf(FileUserUtils.getUser().getId()),recycle,null,tree.getSize(), FilesSizeType.DeleteSize);
            }
            if (StringUtils.equalsIgnoreCase(TreeTypeEnum.RICHTEXT.getType(),tree.getType())){
                //删除富文本文档
                String s = String.valueOf(tree.getFileTreeId());
                sysFormDocService.deleteByTreeNodeId(s,"0");
            }
            ids.add(tree.getFileTreeId());
        }
//         = recycleList.stream().map(FilesTreeRecycle::getFileTreeId).collect(Collectors.toList());
        boolean b = filesTreeService.removeByIds(ids);
        //减少用户已用空间
//        userService.updateStorageLess(String.valueOf(FileUserUtils.getUser().getId()),bigInteger.longValue());
//        fileService.upDateFolderSizeUpDate(String.valueOf(FileUserUtils.getUser().getId()),)
    }



    private List<FilesTreeRecycle> deleteRecycle(List<String> ids) throws BadRequestException {
        LoginUser user = LoginHelper.getLoginUser();
//        FileUserDto user = FileUserUtils.getUser(); //用户信息
        String username = user.getUsername();
        String userId = Long.toString(user.getUserId());

        if (CollectionUtils.isEmpty(ids)) {
            throw  new BadRequestException("错误的 删除对象");
        }

        QueryWrapper<PersonalFilesTree> wrapper = new QueryWrapper<PersonalFilesTree>()
            .eq("delete_id", userId)
            .eq("delete_type",DeleteTypeEnums.DISPLAY.getType())
            .in("id", ids);
        List<PersonalFilesTree> treeList = filesTreeService.list(wrapper);
        if (CollectionUtils.isEmpty(treeList)) {
            throw new BadRequestException("没有要删除的对象");
        }
        return deleteRecycleActual(userId,treeList,true);
    }


    /**
     * @author Yu.
     * @Description // 删除回车站实际
     * @Date 2023/2/8 15:00
     * @param userId
     * @param treeList
     * @param type
     * @return java.util.List<com.cdyd.ecm.modules.recycle.domain.FilesTreeRecycle>
     **/
    private List<FilesTreeRecycle> deleteRecycleActual(String userId,List<PersonalFilesTree> treeList ,Boolean type) {
//        List<PersonalFilesTree> fileList = new ArrayList<>();
        List<FilesTreeRecycle> recycleList = new ArrayList<>();
        List<String> ids = new ArrayList<>();
        treeList.forEach(tree -> {
            FilesTreeRecycle recycle = FilesTreeRecycle.getPersonalRecycle(tree);
            if (StringUtils.equalsIgnoreCase(tree.getType(), TreeTypeEnum.FOLDER.getType())) {
//                ids.add(Long.toString(tree.getId()));
                Long treeId = recycle.getFileTreeId();
//                recycleList.add
                recycleList.add(recycle);
                if (type){
                    List<FilesTreeRecycle> treeRecycles = deleteRecycleActual(userId, String.valueOf(treeId));
                    recycleList.addAll(treeRecycles);
                }
            } else {
                recycleList.add(recycle);
            }
        });
        return recycleList;
    }



    public List<FilesTreeRecycle> deleteRecycleActual(String userId,String id) {
        List<FilesTreeRecycle> treeList = new ArrayList<>();
        QueryWrapper<PersonalFilesTree> wrapper = new QueryWrapper<PersonalFilesTree>()
            .eq("delete_id", userId).eq("parent_id", id);
        List<PersonalFilesTree> list = filesTreeService.list(wrapper);
        list.forEach(tree -> {
            FilesTreeRecycle recycle = FilesTreeRecycle.getPersonalRecycle(tree);
            if (StringUtils.equalsIgnoreCase(tree.getType(), TreeTypeEnum.FOLDER.getType())) {
//                ids.add(Long.toString(tree.getId()));
                Long treeId = recycle.getFileTreeId();
//                recycleList.add
                treeList.add(recycle);
                List<FilesTreeRecycle> treeRecycles = deleteRecycleActual(userId, String.valueOf(treeId));
                treeList.addAll(treeRecycles);
            } else {
                treeList.add(recycle);
            }
        });
        return treeList;
    }






}
